using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using ColliderSystem;
using NFixedPoint;
using UnityEngine;
using UnityEngine.PlayerLoop;

public class Demo : MonoBehaviour
{
    public float speed = 0.1f;
    NSphereCollider player;
    FixedPoint3 input;
    ColliderSyster collisionSystem;

    void Start()
    {
        collisionSystem = new ColliderSyster(
            Application.persistentDataPath + CommonConst.ColliderPath
        );

        //创建一个player的碰撞info
        var info = new SphereInfo()
        {
            uid = transform.GetInstanceID(),
            position = transform.position.ToFixedPoint3(),
            radius = (FixedPoint)(transform.localScale.x / 2)
        };
        player = new NSphereCollider(info);
    }

    private void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        input = new Vector3(h, 0, v).normalized.ToFixedPoint3();
        if (input == FixedPoint3.zero)
        {
            return;
        }
        //移动player
        // player.info.position += input * speed;
        // SetColor();

        collisionSystem.CollisionDetection(player, input * speed);
        SetInfoToView(player);
    }

    void SetInfoToView(NSphereCollider nSphereCollider)
    {
        this.transform.position = nSphereCollider.info.position.ToVector3();
    }

    void SetColor()
    {
        FixedPoint3 posi = FixedPoint3.zero;
        foreach (var shape in collisionSystem.shapeList)
        {
            if (shape is NBoxCollider b)
            {
                if (b.Intersects(player) != null)
                {
                    this.GetComponent<Renderer>().material.color = Color.green;
                    return;
                }
                else
                {
                    this.GetComponent<Renderer>().material.color = Color.blue;
                }
            }
            else if (shape is NSphereCollider s)
            {
                if (player.Intersects(s) != null)
                {
                    this.GetComponent<Renderer>().material.color = Color.green;
                    return;
                }
                else
                {
                    this.GetComponent<Renderer>().material.color = Color.blue;
                }
            }
        }
    }
}
